package com.irdstudio.apicenter.console.controller.make;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.util.MacmsPathUtil;
import com.irdstudio.apicenter.frame.core.jdbc.JdbcBaseDAO;
import com.irdstudio.apicenter.frame.core.jdbc.container.PageInfo;
import com.irdstudio.apicenter.frame.util.BeanUtils;

/**
 * APP 编译与打包
 * 
 * @author zs.ligm
 * @version 1.0
 * @date 2015-07-01
 */
@Controller
@RequestMapping(value = "/make")
public class CompileAndPackAction extends MacmsBaseAction {

	/**
	 * 显示APP的编译与打包环境信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appenv")
	public String appenv(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		request.setAttribute("appid", appId);
		request.setAttribute("appprj", MacmsPathUtil.getMacmsAppPrjPath(appId));
		request.setAttribute("appres", MacmsPathUtil.getMacmsAppResPath(appId));
		request.setAttribute("appsrc", MacmsPathUtil.getMacmsAppResPath(appId));

		return "make/appenv";
	}

	/**
	 * APP编译与打包页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "readyCompile")
	public String readyCompile(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		request.setAttribute("appid", appId);
		request.setAttribute("appprj", MacmsPathUtil.getMacmsAppPrjPath(appId));
		request.setAttribute("appres", MacmsPathUtil.getMacmsAppResPath(appId));
		request.setAttribute("appsrc", MacmsPathUtil.getMacmsAppResPath(appId));

		return "make/compileAndPack";
	}

	/**
	 * APP编译与打包记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appCompileList")
	public String appCompileList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中
		String sql = "select * from app_compile_list where app_id=?";
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		super.context.addDataList("aclList", this.getTableModelDAO().queryList(
				sql, new PageInfo(), new Object[] { appId }));

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);

		return "make/compileList";
	}

}
